﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using NewtifyMe.DataTypes;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            //var xmlPath = "/feed/entry";


            //var xmlDoc = new XmlDocument();
            //var webClient = new WebClient();
            //var stream = new MemoryStream(webClient.DownloadData("http://services.digg.com/2.0/story.getTopNews?type=rss"));
            //xmlDoc.Load(stream);


            //var mgr = new XmlNamespaceManager(xmlDoc.NameTable);
            //mgr.AddNamespace("ns", "http://www.w3.org/2005/Atom");


            //XmlNodeList xmlNode = xmlDoc.SelectNodes(xmlPath, mgr);

            //int count = xmlNode.Count;

            //Console.WriteLine("Count : "+count);

            XmlNode node;



            XNamespace Snmp = "http://www.w3.org/2005/Atom";
            //XDocument RssFeed = XDocument.Load(@"http://services.digg.com/2.0/story.getTopNews?type=rss");



            //var posts = from item in RssFeed.Descendants(Snmp + "entry")
            //            select new Feed
            //            {
            //                Title = item.Element(Snmp + "title").Value,
            //                PublisheDate = item.Element(Snmp + "updated").Value,
            //                Description = item.Element(Snmp + "content").Value,
                           
            //            };


            XDocument xDoc = XDocument.Load(("http://www.rtp.pt/noticias/index.php?tm=4&headline=204&visual=58"));
            XNamespace atom = XNamespace.Get("http://www.w3.org/2005/Atom");
            XNamespace digg = XNamespace.Get("http://digg.com/docs/diggrss/");
            XNamespace media = XNamespace.Get("http://search.yahoo.com/mrss/");

            String title = xDoc.Element(atom + "subtitle").Value;

            Console.WriteLine("Total title is " + title);

            var items = xDoc
                        .Descendants(atom + "item")
                        .Select(x => new 
                        {
                            Title = x.Element(atom + "title").Value,
                            //Link = x.Element(atom + "link").Attribute("href").Value,
                            Category = ""//x.Elements(digg + "category"),
                            //Descripton = x.Element(atom + "content ").Value == null ? "" : x.Element(digg + "content ").Value,
                            //Thumbnail = x.Element(media + "thumbnail").Attribute("url").Value
                        })
                        .ToArray();


            foreach (var feed in items)
            {
                Console.WriteLine("Title "+feed.Title + Environment.NewLine + "     "+feed.Category.Count() );
            }

            //Console.WriteLine(xmlNode.Name + ": " + xmlNode.InnerText);
            Console.ReadKey();
        }


    }
}
